<?php
/*
** 商品分类的操作类
*/
class MerchantCate
{
	protected $_db;

	public function __construct()
	{/*{{{*/
		$this->_name = __CLASS__  ;
		$this->_db = Db::getInstance( Config::$localDb );
	}/*}}}*/

	/*
	** 获取表名
	*/
	public function tableName()
	{
		return "merchant_cate";
	}

	/*
	** 获取列表
	*/
	public function getList($page=0, $size=20, $where="", $order='id')
	{
		$sql = 'select * from ' . $this->tableName();
		if ($where) $sql .= ' where ' . $where;
		if ($order) $sql .= ' order by ' . $order;

		$start = $page * $size;
		$sql .= ' limit ' . $start . ',' . $size;
		$rst = $this->_db->query($sql);
		return $rst ? $rst->fetchAll() : array();
	}

	/*
	** 获取数量
	*/
	public function getCount($where="")
	{
		$sql = 'select count(*) from ' . $this->tableName();
		if ($where) $sql .= ' where ' . $where;
		$rst = $this->_db->query($sql);
		return $rst ? (int)$rst->fetchColumn() : 0;
	}

	/*
	** 添加
	*/
	public function insert(&$data)
	{
		$ok = $this->_db->insert($this->tableName(), array($data));
		if ($ok) {
			$rst = $this->_db->query("SELECT LAST_INSERT_ID()");
			$data['id'] = $rst ? (int)$rst->fetchColumn() : 0;
		}

		return $ok;
	}

	public function getChilds($list, $pid, $depth)
	{
		$tmpList = array();

		if ($pid) $tmpList[$pid] = array('id'=>$pid, 'depth'=>$depth, 'name'=>$list[$pid]['name']);

		foreach ($list as $id=>$row) {
			if ($pid == $row['pid']) {
				$tmpList += $this->getChilds($list, $id, $depth+1);
			}
		}
		return $tmpList;
	}
	
	public function getAll()
	{
		$count = $this->getCount();
		if (!$count) return array();

		$cate_list = array();
		$list = $this->getList(0, $count);
		foreach ($list as $row)
		{
			$cate_list[$row['id']] = $row;
		}

		$list = $this->getChilds($cate_list, 0, 0);
		return $list;
	}

	public function getOptions()
	{
		$html = "";

		$list = $this->getAll();	
		foreach ($list as $row) {
			$space = "";
			for ($i=1; $i<$row['depth']; $i++) {
				$space .= "--";
			}
			$html .= '<option value="'.$row['id'].'">'.$space.$row['name'].'</option>';
		}

		return $html;
	}
}

/*
** 商品信息的操作类
*/
class MerchantGoods
{
	protected $_db;

	public function __construct()
	{/*{{{*/
		$this->_name = __CLASS__  ;
		$this->_db = Db::getInstance( Config::$localDb );
	}/*}}}*/

	/*
	** 获取表名
	*/
	public function tableName()
	{
		return "merchant_goods";
	}

	/*
	** 根据ID获取商品信息
	*/
	public function getOne($id)
	{
		$sql = 'select * from ' . $this->tableName() . " where id=" . $id;
		$rst = $this->_db->query($sql);
		return $rst ? $rst->fetch() : array();
	}

	/*
	** 获取列表
	*/
	public function getList($page=0, $size=20, $where="", $order='id')
	{
		$sql = 'select * from ' . $this->tableName();
		if ($where) $sql .= ' where ' . $where;
		if ($order) $sql .= ' order by ' . $order;

		$start = $page * $size;
		$sql .= ' limit ' . $start . ',' . $size;
		$rst = $this->_db->query($sql);
		return $rst ? $rst->fetchAll() : array();
	}

	/*
	** 获取数量
	*/
	public function getCount($where="")
	{
		$sql = 'select count(*) from ' . $this->tableName();
		if ($where) $sql .= ' where ' . $where;
		$rst = $this->_db->query($sql);
		return $rst ? (int)$rst->fetchColumn() : 0;
	}

	/*
	** 添加
	*/
	public function insert(&$data)
	{
		$ok = $this->_db->insert($this->tableName(), array($data));
		if ($ok) {
			$rst = $this->_db->query("SELECT LAST_INSERT_ID()");
			$data['id'] = $rst ? (int)$rst->fetchColumn() : 0;
		}

		return $ok;
	}

	/*
	** 更改商品库存
	*/
	public function updateStock($id, $value)
	{
		$sql = "update " . $this->tableName() . " set stock = " . $value;
		$sql .= " where id='" . $id ."'";
		return $this->_db->query($sql);
	}
}

/*
** 商品订单的操作类
*/
class MerchantOrder
{
	protected $_db;

	public function __construct()
	{/*{{{*/
		$this->_name = __CLASS__  ;
		$this->_db = Db::getInstance( Config::$localDb );
	}/*}}}*/

	/*
	** 获取表名
	*/
	public function tableName()
	{
		return "merchant_order";
	}

	/*
	** 根据ID获取订单信息
	*/
	public function getOne($id)
	{
		$sql = 'select * from ' . $this->tableName() . " where id=" . $id;
		$rst = $this->_db->query($sql);
		return $rst ? $rst->fetch() : array();
	}

	/*
	** 获取列表
	*/
	public function getList($page=0, $size=20, $where="", $order='id')
	{
		$sql = 'select * from ' . $this->tableName();
		if ($where) $sql .= ' where ' . $where;
		if ($order) $sql .= ' order by ' . $order;

		$start = $page * $size;
		$sql .= ' limit ' . $start . ',' . $size;
		$rst = $this->_db->query($sql);
		return $rst ? $rst->fetchAll() : array();
	}

	/*
	** 获取数量
	*/
	public function getCount($where="")
	{
		$sql = 'select count(*) from ' . $this->tableName();
		if ($where) $sql .= ' where ' . $where;
		$rst = $this->_db->query($sql);
		return $rst ? (int)$rst->fetchColumn() : 0;
	}

	/*
	** 添加
	*/
	public function insert(&$data)
	{
		$ok = $this->_db->insert($this->tableName(), array($data));
		if ($ok) {
			$rst = $this->_db->query("SELECT LAST_INSERT_ID()");
			$data['id'] = $rst ? (int)$rst->fetchColumn() : 0;
		}

		return $ok;
	}
}
?>
